// User.java
package com.estore.model;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

public class User implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

   /**
 * 设置用户积分
 * @param points 积分值，不能为负数
 */
public void setPoints(int points) {
    // 确保积分不会变成负数
    this.points = Math.max(points, 0);
}

    // ... 其余代码保持不变



    public enum UserRole {
        CUSTOMER("普通用户"),
        VIP("VIP用户"),
        ADMIN("管理员");

        private final String description;

        UserRole(String description) {
            this.description = description;
        }

        @Override
        public String toString() {
            return description;
        }
    }

    private final String userId;
    private final String username;
    private String password; // 实际应用中应存储加密后的密码
    private String email;
    private String phone;
    private String address;
    private UserRole role;

    // 新增积分和登录安全相关字段
    private int points; // 用户积分
    private int failedLoginAttempts; // 登录失败次数
    private boolean locked; // 账户是否被锁定
    private LocalDateTime lastFailedLoginTime; // 最后一次登录失败时间
    // 当前登录时间

    public User(String userId, String username, String password, UserRole role) {
        this.userId = userId;
        this.username = username;
        this.password = password; // 实际项目中应加密存储
        this.role = role;
        this.points = 0;
        this.failedLoginAttempts = 0;
        this.locked = false;
    }

    // 原有 getter 和 setter 方法保持不变...
    public String getUserId() { return userId; }

    public String getUsername() { return username; }

    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }

    public String getAddress() { return address; }
    public void setAddress(String address) { this.address = address; }

    public UserRole getRole() { return role; }
    public void setRole(UserRole role) { this.role = role; }


    // 新增积分相关方法
    public int getPoints() { return points; }

    // 新增登录安全相关方法
    public int getFailedLoginAttempts() { return failedLoginAttempts; }
    public void setFailedLoginAttempts(int failedLoginAttempts) { this.failedLoginAttempts = failedLoginAttempts; }
    public void incrementFailedLoginAttempts() { this.failedLoginAttempts++; }

    public boolean isLocked() { return locked; }
    public void setLocked(boolean locked) { this.locked = locked; }

    public LocalDateTime getLastFailedLoginTime() { return lastFailedLoginTime; }
    public void setLastFailedLoginTime(LocalDateTime lastFailedLoginTime) { this.lastFailedLoginTime = lastFailedLoginTime; }



    // 判断会话是否过期 (30分钟)
    // 修改 User.java 文件中的 isSessionExpired 方法
//public boolean isSessionExpired() {
//    // 如果 loginTime 为 null，表示用户未登录或登录时间未设置，不应判定为过期
//    if (loginTime == null) {
//        return false; // 或者根据业务需求返回 false
//    }
//    return LocalDateTime.now().isAfter(loginTime.plusMinutes(30));
//}


    @Override
    public String toString() {
        return String.format("用户ID: %s | 用户名: %s | 角色: %s | 积分: %d | 邮箱: %s | 电话: %s | 地址: %s",
                           userId, username, role, points, email, phone, address);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(userId, user.userId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId);
    }
}
